package com.shujia.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class Demo12Incr {
    private HConnection connection;
    private HBaseAdmin hBaseAdmin;

    @Before
    public void init() {
        // 1、创建连接

        Configuration configuration = new Configuration();

        /*
         * 不需要执行hmaster和regionserver的地址m  hclient会自动去zookeeper中获取，
         * 由于获取到的是一个域名，所以本地必须配置hosts
         *
         */

        //设置zookeeper地址
        configuration.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");

        try {

            //创建连接  对数据增删改查使用
            connection = HConnectionManager.createConnection(configuration);

            /*
             * 创建修改删除表使用
             *
             */
            hBaseAdmin = new HBaseAdmin(connection);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void incr() throws Exception {

        HTableInterface count = connection.getTable(TableName.valueOf("count"));

        //自增
        count.incrementColumnValue("row1".getBytes(), "info".getBytes(), "age".getBytes(), 1L);


        Get get = new Get("row1".getBytes());

        Result result = count.get(get);


        long l = Bytes.toLong(result.getValue("info".getBytes(), "age".getBytes()));
        System.out.println(l);

    }


    @Test

    public void append()throws Exception{
        HTableInterface score = connection.getTable(TableName.valueOf("score"));

        Append append = new Append("row1".getBytes());

        append.add("info".getBytes(),"scores".getBytes(),",77".getBytes());

        score.append(append);


    }

}
